<?php
/*
 * @Descripttion: 关键词
 * @Author: 夜雨
 * @Email: 448408740@qq.com
 * @Date: 2019-11-27 09:51:22
 * @LastEditTime: 2019-12-06 10:13:33
 */
namespace app\admin\controller;

use app\admin\model\Keyword as KeywordModel;
use app\admin\model\Searchword;
use tool\Log;

class Keyword extends Base
{
    // 关键词列表
    public function index()
    {
        if(request()->isAjax()) {

            $limit = input('param.limit');
            $keyword = input('param.keyword');
            $where = [];
            if (!empty($keyword)) {
                $where[] = ['keyword', 'like', $keyword . '%'];
            }

            $model = new KeywordModel();
            $list = $model->getKeyword($limit, $where);

            if(0 == $list['code']) {

                return json(['code' => 0, 'msg' => 'ok', 'count' => $list['data']->total(), 'data' => $list['data']->all()]);
            }

            return json(['code' => 0, 'msg' => 'ok', 'count' => 0, 'data' => []]);
        }

        return $this->fetch();
    }

    // 编辑
    public function edit()
    {
        if (request()->isAjax()) {
            
            $param = input('post.');

            if (empty($param['Keyword'])) {
                return reMsg(-1, '', '请输入关键词');
            }

            $model = new KeywordModel();
            $res = $model->editKeyword($param);

            Log::write("编辑关键词：" . $param['Keyword']);

            return json($res);
        }

        $id = input('param.id');

        $model = new KeywordModel();

        $this->assign([
            'keyword' => $model->getKeywordById($id)['data'],
        ]);

        return $this->fetch();
    }

    // 删除
    public function delete()
    {
        if (request()->isAjax()) {

            $id = input('param.id');

            $model = new KeywordModel();
            $res = $model->delKeywordById($id);

            Log::write("删除关键词：" . $id);

            return json($res);
        }
    }

    // 分析
    public function chart()
    {
        $id = input('param.id');
        $kw = input('param.kw');
        $args['id'] = $id;
        $model = new Searchword();
        $data = $model->chartSearchwordById($args);
        $this->assign([
            'data' => json_encode($data),
            'kw' => urldecode($kw)
        ]);
        return $this->fetch();
    }

}